<?php

namespace app\common\model\Order;

// 引入模型
use think\Model;
use traits\model\SoftDelete;

// 订单的模型
class Order extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $name = 'order';

    // 自动插入的时间戳字段
    protected $autoWriteTimestamp = true;

    // 软删除
    use SoftDelete;

    //  创建时间字段
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    protected $field = false;

    // 追加字段
    protected $append = [
        'status_text',
        // 订单状态
        'createtime_text',
    ];

    public function getStatusTextAttr($value, $data)
    {
        $status = empty($data['status']) ? '' : $data['status'];
        switch ($status) {
            case 0:
                return "未支付";
                break;
            case 1:
                return "待发货";
                break;
            case 2:
                return "已发货";
                break;
            case 3:
                return "已收货";
                break;
            case -1:
                return "退货";
                break;
            default:
                return "未知状态";
                break;
        }
    }

    public function getCreatetimeTextAttr($value, $data)
    {
        $createtime = isset($data['createtime']) ? $data['createtime'] : '';

        if (empty($createtime)) {
            return '';
        }

        return date('Y-m-d H:i', $createtime);
    }

    public function business()
    {
        return $this->belongsTo('app\common\model\Business\Business', 'busid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function admin()
    {
        return $this->belongsTo('app\common\model\Admin', 'shipmanid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function express()
    {
        return $this->belongsTo('app\common\model\Expressquery', 'expressid', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function address()
    {
        return $this->belongsTo('app\common\model\Business\Address', 'businessaddrid', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}